package com.bw.module_merchant.activity

import android.content.Intent
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.bw.lib_base.path.RouterPath
import com.bw.lib_common.view.BaseMvvmActivity

import com.bw.module_merchant.R
import com.bw.module_merchant.adapter.EnterpriseAdapter
import com.bw.module_merchant.databinding.ActivityEnterpriseBinding
import com.bw.module_merchant.vm.EnterpriseViewModel


import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/merchant/MerchantActivity")
class EnterpriseActivity : BaseMvvmActivity<ActivityEnterpriseBinding>() {

    private val viewModel : EnterpriseViewModel by viewModels()
    private val enterAdapter = EnterpriseAdapter()

    override fun initView() {
        super.initView()

        binding.rv.adapter = enterAdapter
        binding.rv.layoutManager = LinearLayoutManager(this)

        binding.enterpriseBack.setOnClickListener {
            finish()
        }

        binding.enterpriseAdd.setOnClickListener {
            startActivity(Intent(this , AddEnterpriseActivity::class.java))
        }

        viewModel.getEnterpriseList()

        enterAdapter.setOnItemClickListener { adapter, view, position ->
            startActivity(Intent(this , EnterpriseDetailActivity::class.java))
            SPUtils.getInstance().put("enterpriseId" , enterAdapter.data[position].id)
//            ARouter.getInstance().build(RouterPath.PATH_SERVICE_PAGE)
//                .withString("enterpriseId" , enterAdapter.data[position].toString())
//                .navigation()
        }


        viewModel.mSuccessLiveData.observe(this){
            enterAdapter.data.addAll(it.data)
            enterAdapter.notifyDataSetChanged()
        }

        viewModel.mErrorLiveData.observe(this){
            ToastUtils.showShort(it)
        }

    }

    override fun bindLayout(): Int = R.layout.activity_enterprise


}